package com.buaa.divinationmaster.algorithm;

import com.buaa.divinationmaster.model.LenormandCard;
import com.buaa.divinationmaster.model.LenormandReading;

import java.util.List;

/**
 * 雷诺曼卡牌解读算法
 * 负责生成不同牌阵的解读内容
 */
public class LenormandInterpreter {
    
    /**
     * 牌阵类型
     */
    public enum SpreadType {
        THREE_CARD("三张牌阵"),
        FIVE_CARD("五张牌阵"),
        NINE_CARD("九宫格牌阵");
        
        private final String displayName;
        
        SpreadType(String displayName) {
            this.displayName = displayName;
        }
        
        public String getDisplayName() {
            return displayName;
        }
    }
    
    /**
     * 执行雷诺曼占卜解读
     */
    public static LenormandReading performReading(String question, SpreadType spreadType, List<LenormandCard> cards) {
        String interpretation = "";
        String timeline = "";
        String advice = "";
        
        switch (spreadType) {
            case THREE_CARD:
                interpretation = interpretThreeCard(cards);
                timeline = generateThreeCardTimeline(cards);
                advice = generateThreeCardAdvice(cards);
                break;
            case FIVE_CARD:
                interpretation = interpretFiveCard(cards);
                timeline = generateFiveCardTimeline(cards);
                advice = generateFiveCardAdvice(cards);
                break;
            case NINE_CARD:
                interpretation = interpretNineCard(cards);
                timeline = generateNineCardTimeline(cards);
                advice = generateNineCardAdvice(cards);
                break;
        }
        
        return new LenormandReading(question, spreadType.getDisplayName(), cards, interpretation, timeline, advice);
    }
    
    /**
     * 三张牌阵解读：过去-现在-未来
     */
    private static String interpretThreeCard(List<LenormandCard> cards) {
        if (cards.size() != 3) return "卡牌数量不正确";
        
        StringBuilder interpretation = new StringBuilder();
        interpretation.append("【三张牌阵解读】\n\n");
        
        interpretation.append("🔮 过去影响：").append(cards.get(0).getName()).append("\n");
        interpretation.append(cards.get(0).getMeaning()).append("\n\n");
        
        interpretation.append("⭐ 当前状况：").append(cards.get(1).getName()).append("\n");
        interpretation.append(cards.get(1).getMeaning()).append("\n\n");
        
        interpretation.append("🌟 未来趋势：").append(cards.get(2).getName()).append("\n");
        interpretation.append(cards.get(2).getMeaning()).append("\n\n");
        
        // 卡牌组合解读
        interpretation.append("【组合含义】\n");
        interpretation.append(interpretCardCombination(cards.get(0), cards.get(1)));
        interpretation.append(" 发展为 ");
        interpretation.append(interpretCardCombination(cards.get(1), cards.get(2)));
        
        return interpretation.toString();
    }
    
    /**
     * 五张牌阵解读：问题-障碍-过去-未来-结果
     */
    private static String interpretFiveCard(List<LenormandCard> cards) {
        if (cards.size() != 5) return "卡牌数量不正确";
        
        StringBuilder interpretation = new StringBuilder();
        interpretation.append("【五张牌阵解读】\n\n");
        
        interpretation.append("❓ 问题核心：").append(cards.get(0).getName()).append("\n");
        interpretation.append(cards.get(0).getMeaning()).append("\n\n");
        
        interpretation.append("⚠️ 主要障碍：").append(cards.get(1).getName()).append("\n");
        interpretation.append(cards.get(1).getMeaning()).append("\n\n");
        
        interpretation.append("📖 过去根源：").append(cards.get(2).getName()).append("\n");
        interpretation.append(cards.get(2).getMeaning()).append("\n\n");
        
        interpretation.append("🔮 未来机会：").append(cards.get(3).getName()).append("\n");
        interpretation.append(cards.get(3).getMeaning()).append("\n\n");
        
        interpretation.append("🎯 最终结果：").append(cards.get(4).getName()).append("\n");
        interpretation.append(cards.get(4).getMeaning()).append("\n\n");
        
        // 整体组合分析
        interpretation.append("【整体分析】\n");
        interpretation.append("从").append(cards.get(2).getName()).append("的过去根源，面对");
        interpretation.append(cards.get(1).getName()).append("的障碍，围绕");
        interpretation.append(cards.get(0).getName()).append("的核心问题，通过");
        interpretation.append(cards.get(3).getName()).append("的未来机会，最终达到");
        interpretation.append(cards.get(4).getName()).append("的结果。");
        
        return interpretation.toString();
    }
    
    /**
     * 九宫格牌阵解读：全面生活分析
     */
    private static String interpretNineCard(List<LenormandCard> cards) {
        if (cards.size() != 9) return "卡牌数量不正确";
        
        StringBuilder interpretation = new StringBuilder();
        interpretation.append("【九宫格牌阵解读】\n\n");
        
        String[] positions = {
            "过去", "现在", "未来",
            "意识层面", "核心问题", "潜意识层面", 
            "外在影响", "建议行动", "最终结果"
        };
        
        for (int i = 0; i < 9; i++) {
            interpretation.append("📍 ").append(positions[i]).append("：").append(cards.get(i).getName()).append("\n");
            interpretation.append(cards.get(i).getMeaning()).append("\n\n");
        }
        
        // 特殊位置组合分析
        interpretation.append("【关键组合】\n");
        interpretation.append("时间线：").append(interpretCardCombination(cards.get(0), cards.get(1), cards.get(2))).append("\n");
        interpretation.append("内在状态：").append(interpretCardCombination(cards.get(3), cards.get(4), cards.get(5))).append("\n");
        interpretation.append("行动路径：").append(interpretCardCombination(cards.get(6), cards.get(7), cards.get(8))).append("\n");
        
        return interpretation.toString();
    }
    
    /**
     * 双卡组合解读
     */
    private static String interpretCardCombination(LenormandCard card1, LenormandCard card2) {
        // 特殊组合规则
        String combo = card1.getName() + "+" + card2.getName();
        
        // 一些经典组合的特殊含义
        if (combo.contains("骑手") && combo.contains("心")) {
            return "爱情消息即将到来";
        } else if (combo.contains("三叶草") && combo.contains("太阳")) {
            return "非常幸运的时期";
        } else if (combo.contains("船") && combo.contains("锚")) {
            return "旅行后的稳定";
        } else if (combo.contains("房子") && combo.contains("戒指")) {
            return "订婚或购房";
        } else if (combo.contains("云") && combo.contains("太阳")) {
            return "困难后的成功";
        }
        
        // 默认组合：综合两张牌的关键词
        return card1.getKeywords().split(",")[0] + "与" + card2.getKeywords().split(",")[0] + "的结合";
    }
    
    /**
     * 三卡或多卡组合解读
     */
    private static String interpretCardCombination(LenormandCard... cards) {
        if (cards.length == 0) return "";
        if (cards.length == 1) return cards[0].getKeywords().split(",")[0];
        if (cards.length == 2) return interpretCardCombination(cards[0], cards[1]);
        
        StringBuilder result = new StringBuilder();
        result.append(cards[0].getKeywords().split(",")[0]);
        for (int i = 1; i < cards.length - 1; i++) {
            result.append("→").append(cards[i].getKeywords().split(",")[0]);
        }
        result.append("→").append(cards[cards.length - 1].getKeywords().split(",")[0]);
        
        return result.toString();
    }
    
    /**
     * 生成时间线预测
     */
    private static String generateThreeCardTimeline(List<LenormandCard> cards) {
        StringBuilder timeline = new StringBuilder();
        timeline.append("近期（1-2周）：").append(cards.get(0).getTiming()).append("的影响\n");
        timeline.append("当前阶段：").append(cards.get(1).getTiming()).append("\n");
        timeline.append("未来发展：").append(cards.get(2).getTiming());
        return timeline.toString();
    }
    
    private static String generateFiveCardTimeline(List<LenormandCard> cards) {
        StringBuilder timeline = new StringBuilder();
        timeline.append("问题时期：").append(cards.get(0).getTiming()).append("\n");
        timeline.append("障碍期：").append(cards.get(1).getTiming()).append("\n");
        timeline.append("机会期：").append(cards.get(3).getTiming()).append("\n");
        timeline.append("结果期：").append(cards.get(4).getTiming());
        return timeline.toString();
    }
    
    private static String generateNineCardTimeline(List<LenormandCard> cards) {
        StringBuilder timeline = new StringBuilder();
        timeline.append("过去阶段已过：").append(cards.get(0).getTiming()).append("\n");
        timeline.append("当前正在经历：").append(cards.get(1).getTiming()).append("\n");
        timeline.append("未来将迎来：").append(cards.get(2).getTiming()).append("\n");
        timeline.append("最终结果：").append(cards.get(8).getTiming());
        return timeline.toString();
    }
    
    /**
     * 生成建议
     */
    private static String generateThreeCardAdvice(List<LenormandCard> cards) {
        return "根据卡牌指引，建议您关注" + cards.get(1).getName() + "所代表的当前状况，" +
               "同时为" + cards.get(2).getName() + "的未来趋势做好准备。";
    }
    
    private static String generateFiveCardAdvice(List<LenormandCard> cards) {
        return "建议重点关注" + cards.get(1).getName() + "代表的障碍，通过" + 
               cards.get(3).getName() + "的机会，最终实现" + cards.get(4).getName() + "的结果。";
    }
    
    private static String generateNineCardAdvice(List<LenormandCard> cards) {
        return "综合分析显示，您的核心问题是" + cards.get(4).getName() + "，" +
               "建议采取" + cards.get(7).getName() + "所代表的行动，" +
               "最终将获得" + cards.get(8).getName() + "的结果。";
    }
}
